package com.yike.user.service.impl;

import com.yike.common.exception.BusinessException;
import com.yike.user.entity.ShopType;
import com.yike.user.mapper.ShopTypeMapper;
import com.yike.user.param.ShopTypeParam;
import com.yike.user.service.ShopTypeService;
import com.yike.user.param.ShopTypePageParam;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yike.common.base.BaseServiceImpl;
import com.yike.common.pagination.Paging;
import com.yike.common.pagination.PageInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 店铺类型表 服务实现类
 *
 * @author Sakura
 * @since 2024-09-04
 */
@Slf4j
@Service
public class ShopTypeServiceImpl extends BaseServiceImpl<ShopTypeMapper, ShopType> implements ShopTypeService {

    @Autowired
    private ShopTypeMapper shopTypeMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveShopType(ShopTypeParam shopTypeParam) throws Exception {
        // 校验名称是否已存在
        LambdaQueryWrapper<ShopType> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShopType::getName, shopTypeParam.getName());
        ShopType shopType = shopTypeMapper.selectOne(wrapper);
        if (shopType != null) {
            throw new BusinessException("名称已存在");
        }
        shopType = new ShopType();
        shopType.setName(shopTypeParam.getName());
        return super.save(shopType);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateShopType(ShopTypeParam shopTypeParam) throws Exception {
        // 获取类型信息
        ShopType shopType = shopTypeMapper.selectById(shopTypeParam.getId());
        if (shopType == null) {
            throw new BusinessException("类型不存在");
        }
        // 校验名称是否已存在
        LambdaQueryWrapper<ShopType> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShopType::getName, shopTypeParam.getName());
        ShopType type = shopTypeMapper.selectOne(wrapper);
        if (type != null && !type.getId().equals(shopTypeParam.getId())) {
            throw new BusinessException("名称已存在");
        }
        shopType.setName(shopTypeParam.getName());
        return super.updateById(shopType);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteShopType(Long id) throws Exception {
        return super.removeById(id);
    }

    @Override
    public Paging<ShopType> getShopTypePageList(ShopTypePageParam shopTypePageParam) throws Exception {
        Page<ShopType> page = new PageInfo<>(shopTypePageParam);
        LambdaQueryWrapper<ShopType> wrapper = new LambdaQueryWrapper<>();
        IPage<ShopType> iPage = shopTypeMapper.selectPage(page, wrapper);
        return new Paging<ShopType>(iPage);
    }

}
